package com.eco.fanliapp.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.common.base.AuthController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppOrderDetail;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.AppUserOrder;
import com.eco.fanliapp.service.IAppOrderDetailService;
import com.eco.fanliapp.service.IAppUserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ProjectName: fanliapp
 * @Package: com.eco.fanliapp.controller.app
 * @ClassName: AppUserOrderController
 * @Author: xiaoxx
 * @Description: 用户订单分佣
 * @Date: 2019/11/28 15:13
 * @Version: 4.2.2
 */
@RestController
@RequestMapping("/app/order")
public class AppUserOrderController extends AuthController {

    @Autowired
    private IAppOrderDetailService orderDetailService ;
    @Autowired
    private IAppUserOrderService appUserOrderService ;

    /**
     *  查询用户订单数量
     * @param unionId
     * @return
     */
    @GetMapping("/user/count")
    public ServerResponse selectAppUserOrderCount(String unionId){
        AppUser user = getAppUser();
        if(null == user) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        QueryWrapper<AppUserOrder> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user.getUserId());
        queryWrapper.eq("order_grade", 1);
        int count = appUserOrderService.count(queryWrapper);

        return  ServerResponse.createBySuccess("查询成功",count);
    }

    /**
     * 订单删除接口
     * @return
     */
    @GetMapping("/deleteOrderDetail")
    public  ServerResponse  deleteOrderDetail(Long orderId){
        AppUser user = getAppUser();
        if(null == user) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        AppOrderDetail appOrderDetail = orderDetailService.selectAppOrderDetailByTradeId(orderId);
        if(appOrderDetail == null){
            return ServerResponse.createByErrorMessage("查无此订单");
        }
        if (appOrderDetail.getTkStatus() == 13 || appOrderDetail.getTkStatus() == 3) {
            int i = orderDetailService.deleteOrderDetail(orderId);
            if (i > 0) {
                return ServerResponse.createBySuccess(i);
            }
        } else {
            return ServerResponse.createByErrorMessage("订单为未失效或者未结算状态，不能删除");
        }
        return ServerResponse.createByError();
    }
}
